// moretunes.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "moretunes.h"
#include ".\moretunes.h"
#include <initguid.h>
#include "moretunes_i.c"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CmoretunesApp


class CmoretunesModule :
	public CAtlMfcModule
{
public:
	DECLARE_LIBID(LIBID_moretunesLib);
	DECLARE_REGISTRY_APPID_RESOURCEID(IDR_MORETUNES, "{E0994E4C-B495-40C0-A8E3-ADBC585E62F0}");
};

CmoretunesModule _AtlModule;

BEGIN_MESSAGE_MAP(CmoretunesApp, CWinApp)
	ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()


// CmoretunesApp construction

CmoretunesApp::CmoretunesApp()
{
	EnableHtmlHelp();
}

CmoretunesApp::~CmoretunesApp() {
	delete tabs;
	delete albumsTab;
	delete coverTab;
	delete reviewsTab;
	delete id3Tab;
	delete prefTab;
	delete aboutTab;
}

// The one and only CmoretunesApp object

CmoretunesApp theApp;


// CmoretunesApp initialization
#define ALREADY_RUNNING "moreTunes is already running on this computer.\n\
Try to open it from the system tray."

BOOL CmoretunesApp::InitInstance()
{
	// Test if the app is already running
	HANDLE hSingle;
	hSingle = ::CreateMutex(NULL, TRUE, m_pszExeName);
	if (ERROR_ALREADY_EXISTS == GetLastError()) {
		AfxMessageBox(ALREADY_RUNNING, MB_ICONINFORMATION, NULL);
		return FALSE;
	}

	AfxOleInit();
	// InitCommonControls() is required on Windows XP if an application
	// manifest specifies use of ComCtl32.dll version 6 or later to enable
	// visual styles.  Otherwise, any window creation will fail.
	InitCommonControls();

	CWinApp::InitInstance();

	AfxEnableControlContainer();
	// Parse command line for standard shell commands, DDE, file open
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);
//	_AtlModule.Init(ObjectMap, hInstance, &LIBID_ATLLib);
	_AtlModule.InitLibId();
//	_AtlModule.dwThreadID = GetCurrentThreadId();
//	_AtlModule.StartMonitor();
//	// Register class factories via CoRegisterClassObject().
	if (FAILED(_AtlModule.RegisterClassObjects(CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE)))
		return FALSE;

	// App was launched with /Embedding or /Automation switch.
	// Run app as automation server.
	if (cmdInfo.m_bRunEmbedded || cmdInfo.m_bRunAutomated)
	{
		// Don't show the main window
		return TRUE;
	}
//	_AtlModule.InitLibId();
	// App was launched with /Unregserver or /Unregister switch.
	if (cmdInfo.m_nShellCommand == CCommandLineInfo::AppUnregister)
	{
		_AtlModule.UpdateRegistryAppId(FALSE);
		_AtlModule.UnregisterServer(TRUE);
		return FALSE;
	}
	// App was launched with /Register or /Regserver switch.
	if (cmdInfo.m_nShellCommand == CCommandLineInfo::AppRegister)
	{
		_AtlModule.UpdateRegistryAppId(TRUE);
		_AtlModule.RegisterServer(TRUE);
		return FALSE;
	}

	// Window and shit creations...

	SetRegistryKey(_T("Feintor"));
	// The tabs and the tab container
	prefTab = new CPrefTab();
	tabs = new CAllTabs(TITLE_MORETUNES, prefTab);

	coverTab = new CCoverTab(prefTab, tabs);
	reviewsTab = new CReviewsTab();
	id3Tab = new CID3Tab(prefTab);
	albumsTab = new CAlbumsTab(tabs, coverTab, reviewsTab, prefTab, id3Tab);
	id3Tab->initPointers(albumsTab);
	aboutTab = new CAboutTab(prefTab);

	tabs->AddPage(albumsTab);
	tabs->AddPage(coverTab);
	tabs->AddPage(reviewsTab);
	tabs->AddPage(id3Tab);
	tabs->AddPage(prefTab);
	tabs->AddPage(aboutTab);

	tabs->SetActivePage(albumsTab);
	tabs->initPages(albumsTab);

	m_pMainWnd = tabs;
	tabs->Create(); // This one is for the release version ;)
//	tabs->DoModal();
	return TRUE;
}




int CmoretunesApp::ExitInstance() {
	_AtlModule.RevokeClassObjects();
	return CWinApp::ExitInstance();
}
